package com.lyc.controller;

import com.lyc.beans.Book;
import com.lyc.beans.Role;
import com.lyc.beans.User;
import com.lyc.service.BookService;
import com.lyc.service.RoleService;
import com.lyc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @Autowired
    private BookService bookService;

    //获取用户列表
    @RequestMapping("/getUser")
    public ModelAndView getUser() {
        ModelAndView m = new ModelAndView();

        List<User> users = userService.getUser();
        System.out.println(users);
        m.addObject("users", users);
        m.setViewName("list");
        return m;
    }


    //登录按钮控制
    @RequestMapping("/login")
    public ModelAndView login(User user) {
        ModelAndView m = new ModelAndView();
        boolean b = userService.login(user);
        if (b) {
            //存在
            List<Book> books= bookService.getBooks();
            m.addObject("books", books);
            m.addObject("msg","登录成功");
            m.setViewName("login");
        }else {
            //不存在
            m.addObject("msg","登陆失败");
            m.setViewName("index");
        }
        return m;
    }

    //注册按钮控制
    @RequestMapping("/register")
    public ModelAndView register(String account, String password) {
        ModelAndView m = new ModelAndView();
        //判断账户是否存在
        if(account != null && password !=null && !account.isEmpty() && !password.isEmpty()){
            //存在，判断是否与数据库账号重复
            boolean b = userService.checkUserByAccount(account);
            if (b){
                //存在
                m.addObject("msg","账户已存在");
                m.addObject("account",account);
                m.addObject("password",password);
                m.setViewName("register");
            }else {
                User user = new User();
                user.setAccount(account);
                user.setPassword(password);
                //添加用户
                userService.addUser(user);

                Role role = new Role();
                role.setUid(account);
                role.setRid(1);
                role.setName("会员");
                //添加角色用户中间表
                roleService.addRole(role);
                m.addObject("msg","注册成功");
                m.setViewName("index");
            }
        }else {
            m.addObject("msg","账号或密码不能为空");
            m.addObject("account",account);
            m.addObject("password",password);
            m.setViewName("register");
        }
        return m;
    }
    //跳转管理员界面
    @RequestMapping("/toLoginShow")
    public String toLoginShow() {
        return "login";
    }

    //注册按钮跳转
    @RequestMapping("/toRegister")
    public String toRegister() {
        return "register";
    }

}
